"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A1检测项确保UNIX域套接字的访问权限配置正确检测与修复成功
Create At   : 2024/10/23
Owner       : lihongji
Description :
    1.设置unix_socket_permissions为0777(所有用户)后检测A1项
    2.修复UNIX域套接字的访问权限不符合安全规范项目后再次检测
    3.设置unix_socket_permissions为0770（仅用户和组）后检测A1项
    4.修复UNIX域套接字的访问权限不符合安全规范项目后再次检测
    5.设置unix_socket_permissions为0000后检测A1项
    6.修复UNIX域套接字的访问权限
Expect      :
    1.设置成功，打印告警信息
    2.修复成功且不打印告警信息
    3.设置成功，打印告警信息
    4.修复成功且不打印告警信息
    5.设置成功，检测合理报错
    6.合理报错
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):
        step = ('----step1:设置unix_socket_permissions为0777(所有用户)后检测A1项 '
                'expect:设置成功，打印告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "unix_socket_permissions=0777")
        self.assertTrue(excute_msg)
        restart_msg = self.sh_primary.restart_db_cluster()
        self.assertTrue(restart_msg)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A1 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure correct access permissions are "
                      "configured for UNIX domain sockets.", check_res,
                      '执行失败' + step)

        step = ('----step2:修复A1不符合安全规范项目后再次检测 '
                'expect:修复成功且不打印告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B1 --detail;'
                          f'gs_checkse -i A1 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure correct access permissions are "
                         "configured for UNIX domain sockets.", check_res,
                         '执行失败' + step)

        step = ('----step3:设置unix_socket_permissions为0770（仅用户和组）后检测A1项'
                'expect:设置成功，打印告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "unix_socket_permissions=0770")
        self.assertTrue(excute_msg)
        restart_msg = self.sh_primary.restart_db_cluster()
        self.assertTrue(restart_msg)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A1 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure correct access permissions are "
                      "configured for UNIX domain sockets.", check_res,
                      '执行失败' + step)

        step = ('----step4:修复A1不符合安全规范项目后再次检测 '
                'expect:修复成功且不打印告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B1 --detail;'
                          f'gs_checkse -i A1 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure correct access permissions are "
                         "configured for UNIX domain sockets.", check_res,
                         '执行失败' + step)

        step = ('----step5:设置unix_socket_permissions为0000后检测A1项  '
                'expect:设置成功，检测合理报错----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "unix_socket_permissions=0000")
        self.assertTrue(excute_msg)
        restart_msg = self.sh_primary.restart_db_cluster()
        self.assertTrue(restart_msg)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A1 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("ERROR: failed to connect", check_res,
                      '执行失败' + step)

        step = ('----step6:修复UNIX域套接字的访问权限  expect:合理报错----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "unix_socket_permissions=0000")
        self.assertTrue(excute_msg)
        restart_msg = self.sh_primary.restart_db_cluster()
        self.assertTrue(restart_msg)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B1 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("ERROR: failed to connect", check_res,
                      '执行失败' + step)

    def tearDown(self):
        step = ('----step6:恢复环境    expect:成功----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "unix_socket_permissions=0700")
        self.assertTrue(excute_msg)
        restart_msg = self.sh_primary.restart_db_cluster()
        self.assertTrue(restart_msg)
        self.log.info(f'----{os.path.basename(__file__)} end----')